package leetcode.dynamic_programming;

/**
 * @author mgl
 * @version 1.0
 * @description TODO leetcode 198.打家劫舍
 * @date 2023/9/27 08:02
 */
public class Rob {

    public int rob(int[] nums) {

        int length = nums.length;
        int[] dp = new int[length];
        dp[0] = nums[0];
        if (length == 1) return dp[0];
        dp[1] = Math.max(nums[0], nums[1]);
        for (int i = 2; i < length; i++) {
            dp[i] = Math.max(dp[i - 1], nums[i] + dp[i - 2]);
        }

        return dp[length - 1];

    }

    public static void main(String[] args) {
        System.out.println(new Rob().rob(new int[]{1, 2, 3, 1}));
    }
}
